class pageManager {
    //构造器
    constructor(showpages, totalpage = 50, getpage) {
        this.pages = document.querySelector(".selectPage")
        this.selectPage = 1
        this.showpages = showpages
        this.totalPage = totalpage
        this.getpage = getpage
        this.render("change")
        this.bindEvent()
    }

    //时间处理
    bindEvent() {
        let _this = this
        _this.getpage.call(_this, _this.selectPage)
        $on(this.pages, "click", ".num choice",function (){
            _this.selectPage = parseInt(this.innerText)
            _this.getpage.call(_this, _this.selectPage)
            if (_this.getIndex(this)===1&&_this.selectPage>=_this.showpages){
                _this.start = _this.selectPage - _this.showpages +1

                _this.render("preview")
            }
            if (_this.getIndex(this)===_this.showpages-1){
                console.log("进入")
                _this.render("change")
            }
        })
        $on(this.pages, "click", ".num", function (e) {
            _this.selectPage = parseInt(this.innerText)
            _this.getpage.call(_this, _this.selectPage)
            if (_this.getIndex(this)===1&&_this.selectPage>=_this.showpages){
                _this.start = _this.selectPage - _this.showpages +1
                _this.render("preview")
                return;
            }
            if (_this.selectPage > _this.totalPage - 2) {
                // console.log("888")
                _this.render()
                return;
            }
            if (_this.getIndex(this) < _this.showpages - 1 && _this.selectPage !== 1) {

                _this.render()
            } else {
                _this.render("change")
            }
        })
    }

    getIndex(dom) {
        let index = 0
        let pageList = document.querySelectorAll(".selectPage .num")
        for (let i = 0; i < pageList.length; i++) {
            if (pageList[i] === dom) {
                index = i
                break;
            }
        }
        return index
    }

    //渲染函数
    render(op) {
        var pageItem = ``

        if(op==='preview'){
            this.start=this.start
            this.end=this.start+this.showpages-1
        }
        //重新渲染
        if (op === 'change') {
            this.start = this.selectPage - 1
            this.end = this.start + this.showpages - 1
        }

        //判断时间
        if (this.start === 0) {
            this.start = 1
            this.end++
        }
        if (this.end >= this.totalPage - 2) {
            this.end = this.totalPage - 2
        }

        //显示第一页
        pageItem += `<a class="num${this.selectPage === 1 ? ' choice' : ''}"><span>1</span></a>`
        //显示前省略号
        if (this.selectPage >= this.showpages - 1 && this.start > 2) {
            pageItem += `<a class="pre"><span>...</span></a>`
        }
        //显示中间部分页码
        for (var i = this.start; i < this.end; i++) {
            pageItem += `<a class="num${i === this.selectPage - 1 ? ' choice' : ''}"><span>${i + 1}</span></a>`
        }
        //显示后省略号
        if (this.selectPage <= this.totalPage && this.end < this.totalPage - 2) {
            pageItem += `<a class="next"><span>...</span></a>`
        }
        //显示后两页
        pageItem += `<a class="num${this.selectPage === this.totalPage - 1 ? ' choice' : ''}"><span>${this.totalPage - 1}</span></a>`
        pageItem += `<a class="num${this.selectPage === this.totalPage ? ' choice' : ''}"><span>${this.totalPage}</span></a>`

        //渲染
        this.pages.innerHTML = pageItem
    }
}